package com.xli.organ.ou.frameou.entity.mapper;

import com.xli.organ.ou.frameou.entity.FrameOu;
import com.xli.organ.ou.frameou.entity.dto.OuAddDTO;
import com.xli.organ.ou.frameou.entity.dto.OuUpdateDTO;
import com.xli.organ.ou.frameou.entity.vo.OuVO;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface IFrameOuMapper {

    IFrameOuMapper INSTANCE = Mappers.getMapper(IFrameOuMapper.class);

    @Mappings({
            @Mapping(source = "ouCode", target = "ou_code"),
            @Mapping(source = "ouName", target = "ou_name"),
            @Mapping(source = "ouShortname", target = "ou_shortname"),
            @Mapping(source = "pid", target = "pid", qualifiedByName = "handlePid"),
            @Mapping(source = "orderNum", target = "order_num")})
    FrameOu toEntity(OuAddDTO dto);

    @Mappings({
            @Mapping(source = "ouCode", target = "ou_code"),
            @Mapping(source = "ouName", target = "ou_name"),
            @Mapping(source = "ouShortname", target = "ou_shortname"),
            @Mapping(source = "orderNum", target = "order_num")})
    FrameOu toEntity(OuUpdateDTO dto);

    @Mappings({
            @Mapping(source = "ou_code", target = "ouCode"),
            @Mapping(source = "ou_name", target = "ouName"),
            @Mapping(source = "ou_shortname", target = "ouShortname"),
            @Mapping(source = "order_num", target = "orderNum")})
    OuVO toVO(FrameOu frameOu);

    @Named("handlePid")
    default Long handlePid(String pid) {
        if (pid == null || pid.isEmpty()) {
            return null;
        }
        return Long.parseLong(pid);
    }
}
